<?php
/**
 * Created by PhpStorm.
 * User: clkj1
 * Date: 2018/7/17
 * Time: 8:54
 */

session_start();
require_once './admin-core/admin-function.php';
$f_file = new fileFunctions();
$f_file->makeDir('./admin-upload/uploads');


if (isset($_FILES['web-logo'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('web-logo', 'web-logo-' . $rand_name, '0');
}
if (isset($_FILES['web-code'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('web-code', 'web-code-' . $rand_name, '0');
}

if (isset($_FILES['about-image1'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-image1', 'about-image1-' . $rand_name, '0');
}
if (isset($_FILES['about-image2'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-image2', 'about-image2-' . $rand_name, '0');
}
if (isset($_FILES['about-image3'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-image3', 'about-image3-' . $rand_name, '0');
}

if (isset($_FILES['about-up'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-up', 'about-up-' . $rand_name, '0');
}
if (isset($_FILES['about-down'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-down', 'about-down-' . $rand_name, '0');
}

if (isset($_FILES['about-head'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('about-head', 'about-head-' . $rand_name, '0');
}

if (isset($_FILES['swipe-image'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('swipe-image', 'swipe-image-' . $rand_name, '0');
}

if (isset($_FILES['index-show-image'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('index-show-image', 'index-show-image-' . $rand_name, '0');
}

if (isset($_FILES['index-news-image'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('index-news-image', 'index-news-image-' . $rand_name, '0');
}

if (isset($_FILES['show-news-image'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('show-news-image', 'show-news-image-' . $rand_name, '0');
}

if (isset($_FILES['add-news-image'])) {
    $rand_name = $f_file->makeOwnValue();
    uploadTempImage('add-news-image', 'add-news-image-' . $rand_name, '0');
}

function uploadTempImage($image_name_g, $image_name_s, $s_id)
{
    $allowedExts = array(
        "gif",
        "jpeg",
        "jpg",
        "png"
    );
    if (isset($_FILES["$image_name_g"])) {
        $image_file = $_FILES["$image_name_g"];
        $image_name = $image_file['name'];
        $image_size = $image_file['size'];
        if ($image_name != '') {
            if ($image_file['error']) {
                exit('{"status":1,"content":"' . $image_file['error'] . '"}');
            }
            if ($image_size > (round(1024 * 1024, 2) * 12)) { //限制上传大小
                exit('{"status":1,"content":"图片大小不能超过12M"}');
            }
            // 限制上传格式
            $temp_image_name_array = explode('.', $image_name);
            $ff_image = end($temp_image_name_array);
            if (!in_array($ff_image, $allowedExts)) {
                exit('{"status":2,"content":"图片格式不对！"}');
            }
            $image_filename = $image_name_s . '.' . $ff_image; //命名图片名称
            //上传路径
            $image_path = './admin-upload/uploads/' . $image_filename;
            $_SESSION["$image_name_g"] = $image_path;
            move_uploaded_file($image_file['tmp_name'], $image_path);
            $size_image = round($image_size / 1024, 2); //转换成kb
            $image_im_size = getimagesize($image_path);
            require_once './admin-core/admin-function.php';
            $admin_sql = new TOAdminSQL();
            $web_set_res = $admin_sql->config_variable_get('./admin-core/config.php', 'config_website_set');
            if ($web_set_res['status'] !== 0) return $web_set_res;
            $web_set_assoc = $web_set_res['assoc'];
            exit('{"status":' . $s_id . ',"url":"' . $web_set_assoc['http'] . "/admin/admin-upload/uploads/" . $image_filename . '","width":"' . $image_im_size[0] . '","height":"' . $image_im_size[1] . '","size":"' . $size_image . '"}');
        }
    }
}